#ifndef UDP_APPENDER_H
#define UDP_APPENDER_H
#include "log_appender.h"
#include "mina/io_handler.h"
#include "mina/udp_connector.h"
#include "mina/udp_connector_config.h"
#include "mina/udp_message_context.h"

#include "ace/OS.h"
#include <memory>
#include <string>
namespace vpm
{
	namespace log 
	{
		namespace detail
		{
			class UdpAppender : public LogAppender, public IoHandler
			{
			public:
				UdpAppender( int port ,const std::string & host):LogAppender(LogConfig::LOGGER_UDP)
				{
					ACE::init();
					m_udpSession = NULL;
					m_udpConnector.reset(new UdpConnector(m_udpConnectorConfig,*this)); 
					m_serverPort = port; 
					m_serverHost = host;
				}

				virtual ~UdpAppender()
				{
				}

				virtual int open()
				{
					m_udpConnector->connect();
					return 0; 
				}

				virtual void close()
				{

				}

				virtual void do_append(LogMessageBlock * pLogMsg)
				{
					if (m_udpSession != NULL)
					{		
						UdpMessageContext msg(pLogMsg->begin(),pLogMsg->length(),m_serverPort,m_serverHost);
						m_udpSession->write(msg);
					}
				}

				virtual void session_opened(IoSession & session)
				{
					m_udpSession = & session; 
				}
			private:
				IoSession * m_udpSession;
				UdpConnectorConfig m_udpConnectorConfig; 
				std::auto_ptr<UdpConnector> m_udpConnector; 
				std::string m_serverHost; 
				int m_serverPort; 

			};
		}

	}
}

#endif // 
